/**
 * @author 汪孟苹
 */
define(['jquery','bbcare','echarts','dm/vm/oxygenPulseFequencyData'],function($,$bb,echarts,$oxygen){
    var oxygenPulseFequency = {};
    oxygenPulseFequency.init = function(){
        var URL = $bb.baseUrl("department")+"followup/service";
        var objInfo=$bb.getPageParams();
        var userId=objInfo.userId;
        $bb.ajax({
            url: URL + "/patient/oxygenPulserate/history",
            type:"post",
            contentType: "application/json;charset=UTF-8",
            dataType:"json",
            data:JSON.stringify({
                data : {
                    "userId": userId
                }
            }),
            success: function(data){
                for(var i=0;i<data.rows.length;i++){
                    var timeLong=data.rows[i].timeLong;
                    data.rows[i].timeLong=$oxygen.timeLong(timeLong);
                }
                $oxygen.listInit({
                    id:"oxygenlistBox",
                    list:data.rows
                });
                $("#detailInfo").html("").css({"height":"auto","padding":"0"});
                $("#oxygenlistBox .bottom-con").eq(0).addClass("active");
                $("#oxygenlistBox .bottom-con").eq(0).find("i").addClass("fff");
                var oxygenPulserateId = data.rows[0].oxygenPulserateId;
                detail(oxygenPulserateId);
                $("#oxygenlistBox .bottom-con").unbind("click");
                $("#oxygenlistBox .bottom-con").click(function(){
                    $(this).addClass("active").siblings(".bottom-con").removeClass("active");
                    $(this).find("i").addClass("fff");
                    $(this).siblings(".bottom-con").find("i").removeClass("fff");
                    var oxygenPulserateId = $(this).attr("oxygenPulserateId");
                    detail(oxygenPulserateId);
                });
                function detail(oxygenPulserateId){
                    $bb.ajax({
                        url: URL + "/patient/oxygenPulserate/detail",
                        type:"post",
                        contentType: "application/json;charset=UTF-8",
                        dataType:"json",
                        data:JSON.stringify({
                            data : {
                                "oxygenPulserateId": oxygenPulserateId
                            }
                        }),
                        success: function(data){
                            objInfo.birthday=data.data.birthday;
                            objInfo.username=nameLens(data.data.userName);
                            $oxygen.userInfoInit({
                                id:"userInfo",
                                list:objInfo
                            });
                            $oxygen.DetailInfoInit({
                                id:"detailInfo",
                                list:data.data
                            });
                            var oxygenPulserateDetails = data.data.oxygenPulserateDetails;
                            var returnVal = $oxygen.lineData(oxygenPulserateDetails);
                            var arrPulseRate = returnVal.arrPulseRate;
                            var arrOxygensaturation = returnVal.arrOxygensaturation;
                            var arrTime = returnVal.arrTime;
                            trend(arrTime,arrPulseRate,arrOxygensaturation);
                            //下方折线图
                            function trend(arrTime,arrPulseRate,arrOxygensaturation){ 
                                var myChart = echarts.init(document.getElementById('trend'));
                                option = {
                                    title: {
                                        text: ''
                                    },
                                    tooltip: {
                                        trigger: 'axis'
                                    },
                                    legend: {
                                        data:['氧饱','脉率']
                                    },
                                    grid: {
                                        top: '10%',
                                        left: '5%',
                                        right: '8%',
                                        bottom: '15%',
                                        containLabel: true
                                    },
                                    xAxis: {
                                        type: 'category',
                                        boundaryGap: false,
                                        data: arrTime
                                    },
                                    yAxis: {
                                        type: 'value'
                                    },
                                    dataZoom: [
                                        {
                                            show: true,
                                            realtime: true,
                                            start:0,
                                            end: 100
                                        },
                                        {
                                            type: 'inside',
                                            realtime: true,
                                            start: 0,
                                            end: 100
                                        }
                                    ],
                                    series: [
                                        {
                                            name:'氧饱',
                                            type:'line',
                                            symbol:'none',  //去掉点  
                                            smooth:true,    //曲线变平滑 
                                            data:arrOxygensaturation
                                        },
                                        {
                                            name:'脉率',
                                            type:'line',
                                            symbol:'none',  //去掉点  
                                            smooth:true,    //曲线变平滑 
                                            data:arrPulseRate
                                        },
                                    ]
                                };
                                myChart.setOption(option);
                            }
                            var timeLong = data.data.timeLong;
                            var oxygenPer80 = data.data.oxygenPer80;
                            var oxygenPer85 = data.data.oxygenPer85;
                            var oxygenPer90 = data.data.oxygenPer90;
                            var oxygenPer94 = data.data.oxygenPer94;
                            var oxygenPer100 = data.data.oxygenPer100;
                            var pulseRateAbnormal = data.data.pulseRateAbnormal;
                            var pulseRateNormal = data.data.pulseRateNormal;
                            var arrOxygenPer = [oxygenPer100,oxygenPer94,oxygenPer90,oxygenPer85,oxygenPer80,pulseRateAbnormal,pulseRateNormal];
                            var arroxygenPerHandle = $oxygen.arrOxygenPerHandle(arrOxygenPer,timeLong);
                            //饼形图
                            timeSharingStatistic(arrOxygenPer,arroxygenPerHandle,timeLong);
                            function timeSharingStatistic(arrOxygenPer,arroxygenPerHandle,timeLong){
                                var myChart = echarts.init(document.getElementById('timeSharingStatistic'));
                                option = {
                                    color:['#9CCC65','#C1E06C','#FFA726','#FF7043','#E84E40','#26C6DA','#E84E40'],
                                    tooltip: {
                                        trigger: 'item',
                                        formatter: "{b}"
                                    },
                                    title:[
                                        {
                                            text:'血氧饱和度（外圈）',
                                            textStyle:{
                                                color:'#7A7A7A',
                                                fontSize :16
                                            },
                                            left:'55%',
                                            top:'15%',
                                        },
                                        {
                                            text:'脉率（内圈）',
                                            textStyle:{
                                                color:'#7A7A7A',
                                                fontSize :16
                                            },
                                            show: true,
                                            left:'55%',
                                            top:'60%',
                                        }
                                    ],
                                    legend: [
                                        {
                                            orient: 'vertical',
                                            itemGap: 5,
                                            textStyle: {
                                                color:'#7A7A7A'
                                            },
                                            left: '55%',
                                            top:'24%',
                                            formatter: function (name) {
                                                return name;
                                            },
                                            data:[arroxygenPerHandle[0],arroxygenPerHandle[1],arroxygenPerHandle[2],arroxygenPerHandle[3],arroxygenPerHandle[4]]
                                        },
                                        {
                                            orient: 'vertical',
                                            itemGap: 5,
                                            textStyle: {
                                                color:'#7A7A7A'
                                            },
                                            left: '55%',
                                            top:"69%",
                                            data:[arroxygenPerHandle[5],arroxygenPerHandle[6],]
                                        }
                                    ],
                                    series: [
                                        {
                                            name:'',
                                            type:'pie',
                                            radius: ['65%', '80%'],
                                            center: ['27%', '50%'], 
                                            label: {
                                                normal: {
                                                    show: false,
                                                }
                                            },
                                            data:[
                                                {value:arrOxygenPer[0], name:arroxygenPerHandle[0]},
                                                {value:arrOxygenPer[1], name:arroxygenPerHandle[1]},
                                                {value:arrOxygenPer[2], name:arroxygenPerHandle[2]},
                                                {value:arrOxygenPer[3], name:arroxygenPerHandle[3]},
                                                {value:arrOxygenPer[4], name:arroxygenPerHandle[4]} 
                                            ]
                                        },
                                        {
                                            name:'',
                                            type:'pie',
                                            radius: ['35%', '58%'],
                                            center: ['27%', '50%'],
                                            label: {
                                                normal: {
                                                    show: false,
                                                }
                                            },
                                            data:[
                                                {
                                                    value:arrOxygenPer[5], 
                                                    name:arroxygenPerHandle[5],
                                                    label: {
                                                        normal: {
                                                            show: true,
                                                            position: 'center',
                                                            formatter:'监测时长 \n',
                                                            textStyle:{
                                                                fontSize: 14,
                                                                color:'#7a7a7a'
                                                            }
                                                        }
                                                    }
                                                },
                                                {
                                                    value:arrOxygenPer[6], 
                                                    name:arroxygenPerHandle[6],
                                                    label: {
                                                        normal: {
                                                            show: true,
                                                            position: 'center',
                                                            formatter:timeLong,
                                                            textStyle:{
                                                                fontSize: 20,
                                                                color: '#7a7a7a'
                                                            }
                                                        }
                                                    }
                                                }
                                            ]
                                        }
                                    ]
                                };
                                myChart.setOption(option);
                            }
                        }
                    });
                }
            }
        });
        $(".content-three").css("display","block");
        //左边内容模块第一个折线图
        $(function(){
            $(window).resize(function(){
                var width=$("#trend").width();
                $("#trend >  div").css({"width":width});
                $("#trend >  div > canvas").css({"width":width});
            });
        });
        function nameLens(name){
            if(name.length>8){
                var username=name.substring(0,8);
            }else{
                username=name;
            }
            return username;
        }
    };
    return oxygenPulseFequency;
});